<script setup>
// import {ref} from "vue";
import {reactive, ref} from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'

import tcom from '../validcode/tcom.vue'
const isRegister = ref(false)



const loginData = ref({
  username: '',
  password: '',
  validcode:poro,
})


const registerData = ref({
  username: '',
  password: '',
  rePassword: '',
})

// 密码校验
const passwordValidator = (rule, value, callback) => {
  if (value == '') {
    callback(new Error('请再次输入密码'))
    // 响应式数据要先.value
  } else if (registerData.value.password != value) {
    callback(new Error(`两次输入密码不同`))
  } else {
    callback()
  }
  return
}


const router = useRouter()

const register = async () => {
  registerData.value.username = registerData.value.username.trim()
  registerData.value.password = registerData.value.password.trim()
  registerData.value.rePassword = registerData.value.rePassword.trim()
  // 发送注册请求
  axios.post("http://localhost:8090/register/"+registerData.value.username+"/"+registerData.value.password+"").then((response) => {
    console.log(response)

    if (response.data == "success") {

      ElMessage.success("注册成功")
      // router.push("/profile")
      // _this.route.path('/main')
    }
    else {
      ElMessage.error("注册失败:用户名重复或者其他原因！！")
    }
  })

  // ElMessage.success("注册成功")
}


const onSubmit = async () => {
  try {
    if (poro===loginData.value.validcode) {

      // 假设这是你的登录API端点
      axios.get("http://localhost:8090/logintest2/"+loginData.value.username+"/"+loginData.value.password+"").then((response) => {
        console.log(response)

        if (response.data === "success") {

          ElMessage.success("登录成功")
          router.push("/profile")
          // _this.route.path('/main')
        }
        else {
          ElMessage.error("登录失败")
        }
      })

    }else {
      ElMessage.error("验证码有误！！！")
    }




// const isRegister =async() =>{
//       console.log("ok")
//     }



  } catch (error) {
    // 请求出错，显示错误信息
    ElMessage.error("请求错误")
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度为3-16位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度为6-16位', trigger: 'blur' },
  ],
  rePassword: [
    {
      required: true,
      validator: passwordValidator,
      message: '两次输入密码不同',
      trigger: 'blur',
    },
  ],
}

import {onMounted} from 'vue'
var poro = "1235"

const randomNum=(min, max)=>{
  return Math.floor(Math.random() * (max - min) + min)
}
const tc = ref(null)
const identifyCodes = '1234567890abcdefghijklmnopqrstuvwxyz';//验证码输入字串
const click_tcom=()=>{
  poro=""
  makeCode(identifyCodes, 4);
  console.log(poro)
  tc.value.setIdentifyCode(poro)
}
onMounted(()=>{
  click_tcom()
})
// 生成随机验证码
const makeCode=(o, l)=> {
  var numCode = ""
  for (let i = 0; i < l; i++) {
    numCode += identifyCodes[
        randomNum(0,identifyCodes.length)
        ]
  }
  poro = numCode
}
console.log(poro)
</script>
<template>
  <el-row class="login-page">
  <el-col :span="12" class="bg"></el-col>
  <el-col :span="6" :offset="3" class="form">

<!--    注册表单-->
    <el-form
        :rules="rules"
        :model="registerData"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
    >
      <el-form-item>
        <h1>注册</h1>
      </el-form-item>

      <el-form-item prop="username">
        <el-input
            v-model="registerData.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="registerData.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="rePassword">
        <el-input
            v-model="registerData.rePassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请再次输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="register" style="width: 100%; height: 40px;">注册</el-button>
      </el-form-item>

      <el-form-item class="flex">
        <el-link type="info" :underline="false" @click="isRegister = false">
          ← 返回
        </el-link>
      </el-form-item>





    </el-form>


<!--    登录表单-->
    <!-- 登录表单 -->
    <el-form
        :rules="rules"
        :model="loginData"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
    >
      <el-form-item>
        <h1>登录</h1>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
            v-model="loginData.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginData.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="validate">
        <div style="display: flex">
          <el-input
              v-model="loginData.validcode"
              :prefix-icon="Lock"

              placeholder="请输入验证码"
          ></el-input>
          <div style="flex: 1" @click="click_tcom">
            <tcom :identifyCode="poro" ref="tc"/>
          </div>
        </div>
      </el-form-item>
<!--      <el-form-item class="flex">-->
<!--        <div class="flex">-->
<!--          <el-checkbox>记住我</el-checkbox>-->
<!--          <el-link type="primary" :underline="false">忘记密码？</el-link>-->
<!--        </div>-->
<!--      </el-form-item>-->
      <!-- 登录按钮 -->
      <el-form-item>
        <el-button
            type="primary"
            @click="onSubmit"
            auto-insert-space
            style="width: 100%; height: 40px"
        >登录</el-button>
      </el-form-item>
      <el-form-item class="flex">
        <el-link type="info" :underline="false" @click="isRegister = true">
          注册 →
        </el-link>
      </el-form-item>

  </el-form>
  </el-col>
  </el-row>
</template>


<style>
/* 你可以在这里添加全局样式，或者直接在组件样式中覆盖 */
.el-button {
  height: 40px; /* 假设这是输入框的高度 */
}

.login-page {
  height: 100vh;
  background-color: #fff;
}
.bg {
  background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
  url('@/assets/login_bg.jpg') no-repeat center / cover;
  border-radius: 0 20px 20px 0;
}

.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  user-select: none;
}

.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.s-canvas {
  height: 38px;
  cursor: pointer;
}
.s-canvas canvas{
  margin-top: 1px;
  margin-left: 8px;
}

</style>
